I have my code:
$db_date2;
$today2 = date("Y-m-d");
$sql1 = "SELECT * FROM todays_spend WHERE id =$user_id_session AND date=$today2";
$date_records = mysql_query($sql1);
while($today_trip=mysql_fetch_assoc($date_records)){
$db_date2 = $today_trip['date'];
}
echo $db_date2;
It is saying "Undefined variable: db_date2". When I remove the AND statement it works. Am I doing the AND statement wrong? the 'date' field in my database is also saved in the format Y-m-d.
also tried the single qoutes '' around the variables, still not working!
Actually it is using your variables as string in the query rather than their values so Just replace your code with the following:
$db_date2;
$today2 = date("Y-m-d");
$sql1 =
$sql1 = "SELECT * FROM todays_spend WHERE id ='".$user_id_session."' AND date='".$today2."'";
$date_records = mysql_query($sql1);
while($today_trip=mysql_fetch_assoc($date_records)){
$db_date2 = $today_trip['date'];
}
echo $db_date2;
For your variables, you'll need single quotes:
This:
$sql1 = "SELECT * FROM todays_spend WHERE id =$user_id_session AND date=$today2";
Should be:
$sql1 = "SELECT * FROM todays_spend WHERE id ='$user_id_session' AND date='$today2'";
You need to write single quote'' around your variable.
Write your query as below:-
$sql1 = "SELECT * FROM todays_spend WHERE id ='$user_id_session' AND date='$today2'";
Warning
mysql_* was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.
Instead, the MySQLi or PDO_MySQL extension should be used
Hope it will help you :-)
Related
I have an error in my script select MySQL with limit. it said
Gagal ambil data:Undeclared variable: $st
this is my script :
include'../konekdb.php';
if(empty($_GET[start]))
{
$st="0";
}
else
{
$st=$_GET[start];
}
$query = 'SELECT * FROM pelanggan LIMIT $st,5';
$ambildata = mysql_query($query);
if(!$ambildata)
{
die('Gagal ambil data:'.mysql_error());
}
include'tabel_pelanggan.php';
mysql_free_result($ambildata);
mysql_close($koneksi);
$query2 = mysql_query("SELECT * FROM pelanggan");
$num = mysql_num_rows($query2);
$hal = ceil($num/5);
echo "Halaman :";
for($i=1;$i<=$hal;$i++){
$page=$i-1;
echo "[][<a href=$_SERVER[PHP_SELF]?start=$page>$i</a>][]";
}
can you help me? thanks before :)
You have an error with you $_GET, you're missing the quotes around the key name.
Your $_GET,
$st=$_GET[start];
What it should be,
$st = $_GET['start'];
Edit 1
You shouldn't be using MySQL now as it is deprecated. Either start using MySQLi or PDO, you should also look in to prepared statements.
Edit 2
Also don't forget to change your if statement too.
From
if(empty($_GET[start]))
To,
if(empty($_GET['start']))
Edit 3
Your query needs to use double quotes or if you want to use single quotes you need to close the single quotes then add the variable on.
From,
$query = 'SELECT * FROM pelanggan LIMIT $st,5';
To,
$query = 'SELECT * FROM pelanggan LIMIT ' . $st . ',5';
Or,
$query = "SELECT * FROM pelanggan LIMIT {$st},5";
while executing the below query i'm not getting sucess value
$name=qwe;
$result = mysql_query("SELECT *FROM USER WHERE name = $name");
Instead of above query if i put the below, I can able to get the appropriate ans:
$result = mysql_query("SELECT *FROM USER WHERE name = 'qwe'");
Can anyone give a solution for my first query??
Close the variable in quote and concatenate the string into query
$name= "qwe";
$result = mysql_query("SELECT *FROM USER WHERE name = '".$name."'");
one more note
please use mysqli for security purposes. Thanks
Use the following:
$result = mysql_query("SELECT *FROM USER WHERE name = '".$name."'");
P.S. I have not tested the above code but it should work.
select * from table_name where col_name = '".$variable."'
Directly use :
$name = 'qwe';
$result = mysql_query("SELECT * FROM USER WHERE name = '$name'");
And on more advise, you should never use mysql_* functions now as they have been deprecated in newer versions of PHP and will produce the notices.
I currently have a mysql_query line that looks like this:
mysql_query("SELECT * from users WHERE id = '". $id1 ."' ") or die(mysql_error());
However, I want to select everything from users where id = $id1 OR $id2. How would I edit this line to do so?
I've thought about just having two copies of this line, one with $id1 and one with $id2. However, I'm sure that there is a more efficent way to accomplish this. I'm very new to SQL.
The answer is almost in your question. You should change the query into this:
mysql_query("SELECT * from users WHERE id = '". $id1 ."' OR id = '" . $id2. "'")
You should however not be using the mysql_* functions anymore, since they are deprecated. Use mysqli_* or PDO instead.
You can use the IN clause just like the example below.
SELECT * FROM employees WHERE id IN ( 250, 220, 170 );
Your query statement can be something like
$query = "SELECT * FROM users WHERE id IN('{$id1}','{$id2}')";
or,
$query = "SELECT * FROM usrs WHERE id = '{$id1}' or id = '{$id2}'";
mysql_* is officially deprecated, so use mysqli or PDO to query your statements.
$results = mysql_query("select * from doctorlist where assignednumber = '1231231234' ");
I need to change the number 1231231234 to a variable. If I change it to the code below it does not work. I have displayed the variable on the page so I know it is set.
$results = mysql_query("select * from doctorlist where assignednumber = '$phoneNumber' ");
Could someone please help. I know it is a small issue, but have been unable to fix it.
Perhaps split it like this
$sql_query = "select * from doctorlist where assignednumber='$phoneNumber'";
$results = mysql_query($sql_query);
or
$sql_query = "select * from doctorlist where assignednumber='".$phoneNumber."' ";
$results = mysql_query($sql_query);
First check your variable type with var_dump($phoneNumber) than do the following:
$results = mysql_query("select * from doctorlist where assignednumber = '".$phoneNumber."' ");
to improve readability and last if you expect an Integer cast your variable like:
(int)$phoneNumber
or if string do
mysql_real_escape_string($phoneNumber)
Try using the variable inside the query like this:
'{$phoneNumber}'
I'm having troubles with a PHP code. The problems come when I execute a SQL query with a PHP variable inside it. The result doesn't shows anything, and the field in the database is not empty. Tried out with a static id (not variable) and the query works fine.
Do you know where I'm doing it wrong?
The query code:
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = `'.$videoSeleccionado.'`;", $conexion);
Try this:
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = `'.$videoSeleccionado.'`;", $conexion) or die(mysql_error());
That will give you an error message.
The problem is that you use both ` and ' as escape characters as the same time.
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = '$videoSeleccionado';", $conexion);
should work.
Often things will be more clear when you echo the query so you can see the final result.
you are using double quotes on your query so there is no need for the dot . operator
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = '$videoSeleccionado'", $conexion);
You are connecting string wrong! You are using ' single quote, but you should use double/none.
Try this query:
mysql_query("SELECT * FROM `videos` WHERE `idvideo` = '$videoSeleccionado';", $conexion)
Or
mysql_query("SELECT * FROM `videos` WHERE `idvideo` = '{$videoSeleccionado}';", $conexion)
Or
$q = "SELECT * FROM `videos` WHERE `idvideo` = '%s';";
mysql_query(sprintf($q, $videoSeleccionado), $conexion)
Edit:
If it's still not working problem can be in query, try checking if it is using mysql_error()(1) or try dumping query(2).
Example(1):
mysql_query("SELECT * FROM `videos` WHERE `idvideo` = '".$videoSeleccionado."';", $conexion) or die(mysql_error());
Example(2):
$q = "SELECT * FROM videos WHERE idvideo = '$videoSeleccionado';";
var_dump($q);
mysql_query($q, $conexion)