简体   繁体   中英

Empty query result in php

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM