Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : Using a variable to hold a table name, and using that variable in queries [duplicate]

I am currently learning PHP, and I'm working on a Registration Form. Somewhere in my code I have these statements

$query = "SELECT `stud_no` FROM `tb_registered_users` WHERE `stud_no`='$studno'";

and

$query = "INSERT INTO `tb_registered_users`
VALUES ('".$studno."','".$firstname."','".$lastname."')";

but instead I want to declare this variable and use it in the queries mentioned above

$mysql_tb = 'tb_registered_users';

So what is the correct syntax for this?

like image 884
ransan32 Avatar asked Dec 01 '12 05:12

ransan32


2 Answers

$query = "INSERT INTO `" . $mysql_tb . "`
VALUES ('".$studno."','".$firstname."','".$lastname."')";
like image 180
nickb Avatar answered Nov 18 '22 22:11

nickb


<?php
$mysql_tb = 'tb_registered_users';
$query = "SELECT `stud_no` FROM `{$mysql_tb}` WHERE `stud_no`='$studno'";
$query = "INSERT INTO `{$mysql_tb}` VALUES ('".$studno."','".$firstname."','".$lastname."')";
like image 10
Yueyu Avatar answered Nov 19 '22 00:11

Yueyu