maplethorpej Posted December 14, 2010 Share Posted December 14, 2010 So, I'm going through a tutorial in my PHP book and we're using the code below. The page shows up and connects to the database (no error reported), but no data comes up. I'm wondering if I'm just missing something small or what? Let me know if you can help :'( <html> <head> <title>Search Results</title> </head> <body> <h1>Book-0-Rama Search Results</h1> <?php //create short variable names $searchType=$_POST['searchType']; $searchTerm=trim($_POST['searchTerm']); if(!$searchType || !$searchTerm){ echo 'You have not entered enough information to process the search'; exit; } if(!get_magic_quotes_gpc()){ $searchType = addslashes($searchType); $searchTerm = addslashes($searchTerm); } echo $searchType; echo $searchTerm; @ $db = new mysqli('localhost','root','root'); if(mysqli_connect_errno()){ echo 'Error: Could not connect to the database man...'; exit; } $query = "SELECT * FROM books WHERE ".$searchType." LIKE '%".searchTerm."%'"; $result = mysqli_query($query); $num_results = mysqli_num_rows($result); echo "<p>Number of books found: ".$num_results."</p>"; for ($i=0;$i<$num_results;$i++){ $row = mysqli_fetch_assoc($result); echo "<p><strong>".($i+1).". Title: "; echo htmlspecialchars(stripslashes($row['title'])); echo "</strong><br/>Author: "; echo stripslashes($row['author']); echo "<br/>ISBN: "; echo stripslashes($row['isbn']); echo "<br/>Price: "; echo stripslashes($row['price']); echo "</p>"; } $result->free(); $db->close(); ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/221583-simple-php-problem-beginner/ Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 ini_set('display_errors',1); error_reporting(E_ALL|E_STRICT); $mysql=mysql_connect($host,$username,$password); mysql_select_db($table,$mysql) or die($mysqlerror); $query='SELECT * FROM TABLE WHERE '.$column.' = \''.$value'\''; echo $query; $result=mysql_query($query) or die(mysql_error()); mysql_close(); $array=array(); while($row=mysql_fetch_array($result)){ $array[]=$row; } print_r($array); foreach($array AS $rowarray){ for($i=0;$i<count($rowarray);$i++){ echo $rowarray[$i]; } } something to look up and mess around with to learn from. and btw, the problem is probably that you never choose database. EDIT: oh and if you used my earlier code from before I edited it, I forgot to add a ). Link to comment https://forums.phpfreaks.com/topic/221583-simple-php-problem-beginner/#findComment-1147019 Share on other sites More sharing options...
maplethorpej Posted December 14, 2010 Author Share Posted December 14, 2010 <?php //create short variable names $searchType=$_POST['searchType']; $searchTerm=trim($_POST['searchTerm']); if(!$searchType || !$searchTerm){ echo 'You have not entered enough information to process the search'; exit; } if(!get_magic_quotes_gpc()){ $searchType = addslashes($searchType); $searchTerm = addslashes($searchTerm); } echo $searchType; echo $searchTerm; $table='books'; $host='localhost'; $username='root'; $password='root'; ini_set('display_errors',1); error_reporting(E_ALL|E_STRICT); $mysql=mysql_connect($host,$username,$password); mysql_select_db($table,$mysql) or die ($mysqlerror); $query='SELECT * FROM TABLE WHERE '.$searchType.' like \''.$searchTerm'\''; echo $query; $result=mysql_query($query) or die(mysql_error()); mysql_close(); $array=array(); while($row=mysql_fetch_array($result)){ $array[]=$row; } print_r($array); foreach($array AS $rowarray){ for($i=0;$i<count($rowarray);$i++){ echo $rowarray[$i]; } } $result->free(); $db->close(); ?> Alright, so this is how I set it up in my code. Now nothing comes up on the page when I go to it... :/ Any thoughts? P.S. Don't worry; I'm not gonna pester about everything but getting this initial connection figured out is pretty big! Link to comment https://forums.phpfreaks.com/topic/221583-simple-php-problem-beginner/#findComment-1147035 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 probably having problems connecting to mysql... or choosing the database. if($mysql=mysql_connect($host,$username,$password)){ echo 'success connecting to mysql'; }else{ echo 'couldn\'t connect to mysql'; } if(mysql_select_db($db,$mysql)){ echo 'success choosing database.'; }else{ echo 'couldn\'t choose db'; } oh and I did a typo, I meant $db, not $table. $db is the database name you want to choose... and the $mysqlerror can be whatever you want it to be. Link to comment https://forums.phpfreaks.com/topic/221583-simple-php-problem-beginner/#findComment-1147038 Share on other sites More sharing options...
maplethorpej Posted December 15, 2010 Author Share Posted December 15, 2010 Below is the current, edited script. Unfortunately, nothing comes up... Not even the "Book-o-Rama Search Results" heading within the <h1> </h1> tags. What's going on?? I can't find the error <html> <head> <title>Search Results</title> </head> <body> <h1>Book-0-Rama Search Results</h1> <?php //create short variable names $searchType=$_POST['searchType']; $searchTerm=trim($_POST['searchTerm']); if(!$searchType || !$searchTerm){ echo 'You have not entered enough information to process the search'; exit; } if(!get_magic_quotes_gpc()){ $searchType = addslashes($searchType); $searchTerm = addslashes($searchTerm); } echo $searchType; echo $searchTerm; $db='books'; $host='localhost'; $username='root'; $password='root'; ini_set('display_errors',1); error_reporting(E_ALL|E_STRICT); if($mysql=mysql_connect($host,$username,$password)){ echo 'success connecting to mysql'; }else{ echo 'couldn\'t connect to mysql'; } if(mysql_select_db($db,$mysql)){ echo 'success choosing database.'; }else{ echo 'couldn\'t choose db'; } $query='SELECT * FROM TABLE WHERE '.$searchType.' like \''.$searchTerm'\''; echo $query; $result=mysql_query($query) or die(mysql_error()); mysql_close(); $array=array(); while($row=mysql_fetch_array($result)){ $array[]=$row; } print_r($array); foreach($array AS $rowarray){ for($i=0;$i<count($rowarray);$i++){ echo $rowarray[$i]; } } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/221583-simple-php-problem-beginner/#findComment-1147486 Share on other sites More sharing options...
maplethorpej Posted December 15, 2010 Author Share Posted December 15, 2010 I messed around with it a little more and found out that $query='SELECT * FROM TABLE WHERE '.$searchType.' like \''.$searchTerm'\''; echo $query; was breaking the script. When those lines were removed, something would show up again. Any thoughts? Link to comment https://forums.phpfreaks.com/topic/221583-simple-php-problem-beginner/#findComment-1147490 Share on other sites More sharing options...
Pikachu2000 Posted December 15, 2010 Share Posted December 15, 2010 Is the $searchType value expected to be numeric? Link to comment https://forums.phpfreaks.com/topic/221583-simple-php-problem-beginner/#findComment-1147498 Share on other sites More sharing options...
MMDE Posted December 15, 2010 Share Posted December 15, 2010 I messed around with it a little more and found out that $query='SELECT * FROM TABLE WHERE '.$searchType.' like \''.$searchTerm'\''; echo $query; was breaking the script. When those lines were removed, something would show up again. Any thoughts? $query='SELECT * FROM TABLE WHERE '.$searchType.' like \''.$searchTerm.'\''; ^ missed a dot after $searchTerm Link to comment https://forums.phpfreaks.com/topic/221583-simple-php-problem-beginner/#findComment-1147500 Share on other sites More sharing options...
maplethorpej Posted December 15, 2010 Author Share Posted December 15, 2010 I finally got it to work after I actually dug in and did some troubleshooting on my own. Sorry for the ignorance guys! By the way, that youTube video was perfect lol! I'll be more specific from now on Link to comment https://forums.phpfreaks.com/topic/221583-simple-php-problem-beginner/#findComment-1147501 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.