Jump to content

Simple PHP problem (beginner)


maplethorpej

Recommended Posts

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

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 ).

<?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!

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.

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  :wtf:

 


<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>


 

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.