/    Sign up×
Community /Pin to ProfileBookmark

Need help displaying data from a mysql query

If I search a database and it returns multiple results, what should I use to grab each result so that I can display each one in the browser. Below is the search and I thought I would use the num_rows variable to how a message if no results are found.

[code=php]
$result = mysql_query(“SELECT isbn, bookTitle, bookAuthor, thumbnail FROM BooksForSell WHERE ISBN='”.$booksearch.”‘”) or die(mysql_error());
$num_rows = mysql_num_rows($result);
[/code]

While the below is not quite formatted the way it will end up looking, I basically want to have a for loop go through the results and show each one in HTML similar to below. I assume that maybe an array would be best for this but I am not sure what the function is to get the results into an array.

[code=php]

echo ‘
Title: ‘ .$title. ‘
Author: ‘ . $author . ‘
ISBN: ‘ . $isbn . ‘
Price: ‘ .$price’
Condition: ‘. $condition . ‘
<img src=”.$thumbnail.”>’;

[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@wevieauthorNov 30.2013 — So here is where I am not. I found most of the following code at php net but I am still not getting anywhere. Am I even close to doing what I need to do? If I am being honest, I am not really familiar with objects in PHP so that is not helping matters in this case.

[code=php]
if(mysqli_connect_errno()){

echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT isbn, bookTitle, bookAuthor, thumbnail FROM BooksforSell WHERE isbn='".$bookSearch."'";
$result = $mysqli->query($query);
while($newArray = $result->fetch_array()){
$isbn = $newArray['isbn'];
$bookTitle = $newArray['bookTitle'];
$bookAuthor = $newArray['bookAuthor'];
$thumbnail = $newArray['thumbnail'];

echo $isbn;
echo $bookTitle;
echo $bookAuthor;
echo $thumbnail;
}
[/code]
Copy linkTweet thisAlerts:
@wevieauthorNov 30.2013 — And this should have been the first line in the code only with my database info

[code=php]
$mysqli = new mysqli("localhost","user,"password","database");
[/code]
Copy linkTweet thisAlerts:
@firesnakerNov 30.2013 — You have to create a storage array.
<i>
</i>$aStorage = new Array();
while($newArray = $result-&gt;fetch_array()){
$aStorage[] = array(
'isbn' =&gt; $newArray['isbn'];
'bookTitle' =&gt; $newArray['bookTitle'];
'bookAuthor' =&gt; $newArray['bookAuthor'];
'thumbnail' =&gt; $newArray['thumbnail'];
);
}

Then, somewhere else, you create a loop calling the storage array.
<i>
</i>for ($i = 0; $i &lt; count($aStorage); $i++)
{
echo $aStorage[$i]['isbn'];
echo $aStorage[$i]['bookTitle'];
echo $aStorage[$i]['bookAuthor'];
echo $aStorage[$i]['thumbnail'];
}


If I may suggest you read up the manual section on Array to get a better understanding of the code.

http://us1.php.net/manual/en/language.types.array.php

In short, an array is a pair of key and value. You call the key to retrieve the value.

Now, to store the data you need a multidimensional array. To be specific, a two-dimensional array.

This should be enough to get you started.

Three-dimensional array and beyond are possible, but rarely used in my experience.

Good luck!
Copy linkTweet thisAlerts:
@alinarcosNov 30.2013 — <?php$username="username";

$password="password";$database="your_database";

$field1-name=$_POST['Value1'];

$field2-name=$_
POST['Value2'];

$field3-name=$_POST['Value3'];

$field4-name=$_
POST['Value4'];

$field5-name=$_POST['Value5'];

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO tablename VALUES('','$field1-name','$field2-name',

'$field3-name','$field4-name','$field5-name')";mysql_query($query);mysql_close();?>
Copy linkTweet thisAlerts:
@wevieauthorNov 30.2013 — I tried the code that firesnaker and it did not work. new Array(); seems to be incorrect syntax because that gave me an error. After looking around on php.net, I changed it to just array(), but I was still not getting any data into the array I created.


I have changed my code a bit since the object oriented style is not really making sense to me anyway but I still do not understand how to get the data. I think my problem is that I do not understand how it is returned.

[code=php]
//creates a query
$query = "SELECT isbn, bookTitle, bookAuthor, bookCond, thumbnail, price FROM BooksforSell WHERE isbn='".$bookSearch."'";
//executes a query and returns results
$result = mysqli_query($dbConnect, $query);
//assigns the number of rows returned from the query
$numRows = $result->num_rows;
$row = mysqli_fetch_array($result, MYSQLI_NUM);
[/code]


The query I am running in the test returns three rows. I have been looking at php.net and on this if I echo $row[0], $row[1], ......, that will output the first row of data. What I am not understanding is how are the rest of the rows returned? Maybe if I understood that, I could get it.
Copy linkTweet thisAlerts:
@wevieauthorDec 01.2013 — Just thought I would update. I have it working, finally but I did run into one really weird thing that I never did figure out. Below is the code to make the array.

[code=php]
$query = "SELECT isbn, bookTitle, bookAuthor, bookCond, thumbnail, price FROM BooksforSell WHERE isbn='".$bookSearch."'";
//executes a query and returns results
$result = mysqli_query($dbConnect, $query);
//assigns the number of rows returned from the query
$numRows = $result->num_rows;

$i = 0; //create index and set to 0
$bookInfo = array(); //create a new empty array
//iterate through and fill array with data
while($row = mysqli_fetch_array($result, MYSQLI_NUM)){

$bookInfo['isbn'][$i] = $row[0];
$bookInfo['title'][$i] = $row[1];
$bookInfo['author'][$i] = $row[2];
$bookInfo['condition'][$i] =$row[3];
$bookInfo['thumbnail'][$i] = $row[4];
$bookInfo['price'][$i] = $row[5];

$i++;

}//end while loop
[/code]


The weird thing is when I ran the for loop, the output was always twice the index of the array. I have been testing with a query that returns three results so when the for loop ran, it would run six times with the following code.

[code=php]for($i=0; $i < count($bookInfo); $i++)[/code]

I though maybe is was because I used $i in the while loop but it should be local. Either way, I changed the $i to $j and it still did the same thing. The only way I found to get it to work is by using the row count which I set earlier like so.

[code=php]for($i=0; $i < $numRows; $i++)[/code]

Any ideas as to why this was happening?
×

Success!

Help @wevie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...