[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

php sql variable database extracting data question

hello i have a variable
$show that is 'img/affiche1.gif'
i want to extract all the data in my database where the field image is egal to this variable

i got this error using the code i give u

Notice: Undefined variable: sql in C:\wamp\www\print.php on line 108
Erreur SQL !Unknown column 'img' in 'where clause'

//////////////////////////////// sql creation table

CREATE TABLE IF NOT EXISTS `image` (
  `cat` int(11) unsigned NOT NULL DEFAULT '0',
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `s_cat` int(10) unsigned NOT NULL,
  `chemin` varchar(100) NOT NULL,
  `image` varchar(60) NOT NULL,
  `movie` varchar(80) NOT NULL,
  `name` varchar(30) NOT NULL DEFAULT '',
  `infos-fr` text NOT NULL,
  `infos-en` text NOT NULL,
  PRIMARY KEY (`id`),
  KEY `cat` (`cat`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=53 ;

////////////////////////////////////////////// DATA
INSERT INTO `image` (`cat`, `id`, `s_cat`, `chemin`, `image`, `movie`, `name`, `infos-fr`, `infos-en`) VALUES
(1, 1, 1, '/img/imagette_10.gif', 'img/affiche1.gif', '', '', 'fr text ', 'en text '),
(1, 2, 1, '/img/imagette_logo1.gif', 'http://www.test.com', '', '', 'logo pour un laboratoire d''analyses médicales', 'logo for an analysis laboratory '),
(1, 3, 1, '/img/imagette_logo2.gif', '', '', '', 'projet pour une ONG ', 'project for an NGO'),
(1, 4, 2, '/img/imagette_affiche1.gif', '/img/affiche1.gif', '', '', 'carte postale pour un animateur en pâte à modeler', 'postcard for an animator'),
(1, 5, 2, '/img/imagette_affiche2.jpg', '', '', '', 'Concours Chaumont non au réchauffement climatique.', 'Chaumont stop global warming'),
(1, 6, 2, '/img/imagette_affiche3.gif', '', '', '', 'affiche publicitaire', 'board ');

thanks

<?php
if ($_GET['show'] == '') $show = 0;
	else $show = $_GET['show'];

if ($show != '0') {
   echo "<p id='info'>test</p>";
	echo $show;   
    $requete3 = mysql_query('SELECT *  FROM image WHERE image= '.$show.' ')OR die('Erreur SQL !'.$sql.''.mysql_error());

	while($resultat = mysql_fetch_assoc($requete3))
     { echo $resultat['chemin'];
	 } }
     else 
	 {echo "<p id='info'>on n'est pas sur l'affichage spécifique d'image</p>";
	
	 	 }

Open in new window

0
jerrrrry
Asked:
jerrrrry
  • 5
  • 4
2 Solutions
 
zicevaCommented:
Replace
$requete3 = mysql_query('SELECT *  FROM image WHERE image= '.$show.' ')OR die('Erreur SQL !'.$sql.''.mysql_error());

with

$requete3 = mysql_query('SELECT *  FROM image WHERE image= \''.$show.'\' ')OR die('Erreur SQL ! '.mysql_error());
0
 
zicevaCommented:
Please note that there is no double quote .. there are all single quotes with one escaped single quote on either side of the php variable ...
0
 
jerrrrryAuthor Commented:
ok i remplace it with your suggestion  but nothing is displaid
i should have $resultat['chemin'] displayed?
do u have a idea?
tks
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
zicevaCommented:
Before the while statement add a

echo mysql_num_rows($requete3);

to see if anything get's selected ...
0
 
jerrrrryAuthor Commented:
0 is displaid
0
 
zicevaCommented:
Is the $_GET['show'] variable set correctly? Display that too as a debug method ...

echo $_GET['show'];

Also, you can set it statically for debug purposes also ...
0
 
jerrrrryAuthor Commented:
i got this up the script

 if ($_GET['show'] == '') $show = 0;
      else $show = $_GET['show'];
      echo $show;
it displays this:
img/affiche1.gif
0
 
jerrrrryAuthor Commented:
do you have an idea?
i gave you the sql data it should display something, isn't it
0
 
Ray PaseurCommented:
Let's go back and look at these messages:

Notice: Undefined variable: sql in C:\wamp\www\print.php on line 108
Erreur SQL !Unknown column 'img' in 'where clause'

My first concern is that the Notice occurred on line 108, but you have not posted 108 lines of code, so we are probably going to have to guess what the 108 lines of code were doing.  That is easy to remedy.  Post the entire code segment, just exactly the way you have it so we can see how the variable $sql is created.  Or if it is not created (and the Notice seems to suggest that it is not created, or perhaps $sql is created and then unset() later in the code) maybe we can find out what you wanted to have in that variable.  Probably it was a query string, right?

My second concern is that the message refers to a column named 'img' in a WHERE clause.  However the query you posted does not use a column name of 'img' -- it uses 'image' instead.  This is the kind of error that occurs when you are running the script out of the wrong directory, using an old copy of the script or something like that.  Check your paths to the script and make sure you've got the right one.
0
 
jerrrrryAuthor Commented:
ziceva ok it works 145 points
pay paseur earns the 20 additional points for gentle participation
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now