1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP / MySQL Prepared statement - table name as variable

Discussion in 'PHP' started by karl_murphy, Feb 24, 2014.

  1. #1
    Hi all,

    I was wondering if it is possible to create a prepared statement where the table name is a variable? I have written the following which works fine, but want to know if I can interchange the sql table with a variable:

    $status = 'Y';
    if($get_rows = $mysqli->prepare("SELECT id, field1, field2, field3, field4, field5, field6 FROM myTable WHERE field7=? ORDER BY field8 DESC"));
    // Bind a variable to the parameter as an integer.
    $get_rows->bind_param("s", $status);

    // Execute the statement

    // Store result

    // Get the variables from the query.
    $get_rows->bind_result($id, $field1, $field2, $field3, $field4, $field5, $field6);

    // While the rows are being returned
    while ($get_rows->fetch())

    Any help would be greatly appreciated.

    karl_murphy, Feb 24, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Likes Received:
    Best Answers:
    Trophy Points:
    PoPSiCLe, Feb 24, 2014 IP