SQL/HTML - Statements wont load on the HTML page - html

Me and a friend of mine are working on a school project. We only have a little problem with implementing the SQL into HTML. And as our informatics teacher is of no help, I hope you are.
Our problem is the following.
We have coded our SQL part into the HTML, also guided by online tutorials...
Now our teacher sorted it to this:
<?php
mysql_connect("Localhost", "root", "usbw") or die (mysql_error());
mysql_select_db('aanbod');
$query = "SELECT merk, model FROM Aanbod";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row["merk"] . "</td>
<td>" . $row["model"] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
And still getting this error, now on the webpage:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\USBWebserver v8.67\root\merkmodelshow.php on line 141
Supposing the problem is in this line:
while($row = mysql_fetch_array($result)){
If anyone could help us with this project. It's due soon :C
Regards,
Demiƫn en Bo.

use missed closing ;
try
<?php
mysql_connect("Localhost", "root", "usbw") or die (mysql_error ());
mysql_select_db('aanbod');
$query = "SELECT 'a'=COUNT(nr) FROM Aanbod";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['a'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
But now the site displays this error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\USBWebserver v8.67\root\merkmodelshow.php on line 141
This suggests the problem is in this line:"
while($row = mysql_fetch_array($result)){
Though we weren't able to resolve the problem [yet]
change your query to
$query =" SELECT *, ( select COUNT(nr) FROM Aanbod) as a from Aanbod";

Related

How to make reference to one of my MySQL databases inside my PHP file?

I have already asked this related question: https://webmasters.stackexchange.com/questions/116055/using-mysql-database-data-directly-into-generating-articles-for-my-website-new/116056?noredirect=1#comment154341_116056
At this point, I'm starting to understand the code syntax and project structure a little better.
But I have made my database using MySQL console. it only has a few entries so far, I wanted to try to adapt the code in Zach's example, but here is the problem I have:
The problem is, I am unsure how to get the reference to my database object? In the code sample from Zach there is variable $db, I guess this is where i need to keep a reference to my own actual database.
Here is the psuedo-code (maybe) from Zach, note: he always said to me not to copy-paste it, but I'm just trying to see how I can use it in my project.
<?php
$SQL_Query = "SELECT * FROM your_table";
$SQL_Run = mysqli_query($db, $SQL_Query);
while ($row = mysqli_fetch_assoc($SQL_Run)) {
echo
"<section class='wrapper style1'>
<div class='inner'>
<header class='align-center'>
<h2>" . $row['imageurl'] . "</h2>
<img src='" . $row['title'] . "'>
<p>" . $row['description'] . "</p>
</header>
</div>
</section>";
}
?>
So my question simply at moment is: How to create the reference $db?
Your answer is the correct way to establish a connection. I want to point out that there are two ways of writing that statement. The version you found online is one way, but from our previous conversation, you can write it like this:
<?php
// Establish how to log in
$servername = "127.0.0.1";
$username = "root";
$password = "yourpasswordhere";
$dbname = "yourdatabasenamehere";
// create the database connection
$db = new mysqli($servername, $username, $password, $dbname);
// if it fails, kill the site.
if (mysqli_connect_error($db)) {
die("Connection failed: " . mysqli_connect_error($db));
}
// your first query to grab all the article data
$SQL_Query = "SELECT * FROM your_table";
// run the query
$SQL_Run = mysqli_query($db, $SQL_Query);
// while data exists (it makes sure that you have post data, otherwise nothing shows up)
while ($row = mysqli_fetch_assoc($SQL_Run)) {
echo
"<section class='wrapper style1'>
<div class='inner'>
<header class='align-center'>
<h2>" . $row['imageurl'] . "</h2>
<img src='" . $row['title'] . "'>
<p>" . $row['description'] . "</p>
</header>
</div>
</section>";
}
// Close the connection
mysqli_close($db);
?>
You will notice that the connections are written like a function.
mysqli_num_rows($result);
instead of
$result->num_rows
Both do the same thing, just a personal preference. That should hopefully clear some things up from your first post :)
I have got further on and I think have answered my own question. I found it a bit tricky to research because I don't understand all the different terms and names of features/api/scripts/etc. But I had just to read the documentation for mysqli_connect(), I set up the code as follows and now I have pulled all the data from the database into words on my html/php files.
From here I think I can rewrite the code to first sort it by date and then can of course put the latest posts at the top of each page etc.
I can also allow the user to click 'Genre' and only view Comedy for example.
Here is the code just to get the data parsed into my index.php file:
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "yourpasswordhere";
$dbname = "yourdatabasenamehere";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, type, title FROM releases";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Type: " . $row["type"]. " - Title " . $row["title"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I extended upon the above work by making the php script fetch all the entries in the database and create the previous html article I had once for each entry. In the SELECT statement I can control which types of entries are displayed (eg. For a certain category). Here was how I did it:
// make an html article based snippet (image, title, description, etc),
//once for each entry in the database table...
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "somepassword";
$dbname = "somedatabasename";
// create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM releases ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo '<section class="wrapper style1">';
echo '<div class="inner">';
echo '<header class="align-center">';
echo '<h2>'. $row["title"] . '</h2>';
echo '<div class="image fit">';
echo '<img src='. $row["imgurl"] .'>';
echo '</div> <p> RELEASE TITLE: ' . $row["title"] . '<br /> DATE POSTED: ' . $row["postdate"] . '<br /> DESCRIPTION: ' . $row["description"] . '</p>';
echo 'DOWNLOAD LINK: '.$row["link"].' <br />';
$NfoLink = $row["nfolink"];
if ($NfoLink != 'not found' && $NfoLink != '')
{
echo 'NFO LINK/MORE DOWNLOADS: '.$row["nfolink"].'';
}
echo '</header>';
echo '</div>';
echo '</section>';
}
}
else
{
echo "0 results";
}
$conn->close();
?>

Create a div on my website that shows database information with php

I need to create a div that only shows the information of the database, how should i connect the div to mysql so it only shows my database information?
I'm not sure I'm understanding your question... but I'll give it a try. The code below this will select whatever you tell it to from the database and echo it into a table.
<?php
$db = mysqli_connect('host', 'username', 'password', 'database');
$sql = "SELECT * FROM database"
mysqli_stmt_bind_param($db, "params here", your info here);
$res = mysqli_stmt_execute($sql);
mysqli_stmt_close($sql);
mysqli_close($db);
echo "<table>";
while($row = mysqli_fetch_array($res)){ //Creates a loop to loop through results
echo "<tr><td>" . $row[''] . "</td><td>" . $row[''] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>";
?>

how to display images whose names are stored in database

I had created a table in mysql database. I am using php One column was for images, but it only stored images name. I wanted to print the whole table so did the coding but got got only the name of the images in the table.
Please explain how can I display images on the webpage that are stored in my PC. I don't want to save images in the database.
I used this code to print the table :
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$query="SELECT * FROM images LIMIT 0,5";
$result = #mysql_query($query);
echo "<table border='1'>
<tr>
<th>S.No.</th>
<th>Image</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['image_id'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Hope you got some answer....
All your code is doing is sending the filename to the screen as a string. You need to the use image tags.
You may find this page useful, http://www.htmlcodetutorial.com/images/_IMG.html

How to repeat different pic?

I wan to always display the different pic. So there's a problem with looping the different pic. How do i do that?
index.php:
<?php
$result = mysql_query("SELECT * FROM table2", $connection );
echo '<ul>';
while($row = mysql_fetch_array($result)) {
if ($row)
echo "<li>";
echo "<img src='code.php?id=3'>";
echo "&nbsp";
echo "</p>";
echo "</li>";
echo "<br/>" ;
}
echo '</ul>';
mysql_close($connection);
?>
You need to pull the id value from the database. If you have a column called, maybe id you would want to put:
while($row = mysql_fetch_array($result)){
if ($row){
echo "<li>";
echo "<img src='code.php?id=".$row['id']."'/>"; // see what I did there?
echo "&nbsp";
echo "</p>"; // take this out
echo "</li>";
echo "<br/>"; // take this out
}
}
P.S. - don't write new code using the deprecated mysql extension. Use PDO or mysqli at least. And don't put <br />s between your <li>s, or close unopened <p>s. And, generally speaking, don't store your images in your database - just store the path to the image and put the images themselves in a folder on the server where they belong.
And please format your code - it is hard to read with no indentation or separation of files (your if statement was not properly enclosing what should have been conditioned statements). And mysql_real_escape_string is not as cool as you think it is.
Hope this helps.

using mysqli instead of mysql accessing the DB

please find below a script in php that that fetch data from my database company, the database is in phpMyadmin
I understand that there are secure ways to fetch data. My question is
Can I improve the code below using mysqli to fetch instead of mysql_query.
<?php
get results from database. Can I improve the code below using mysqli to fetch instead of mysql_query in the following line?.
$result = mysql_query("SELECT * FROM customer")
or die(mysql_error());
tables begins here with the customer id and customer name
echo "<table >";
echo "<tr>
<th>ID</th> the customer id
<th>Name</th> the customer name
</tr>";
// loop through results of database query, displaying them in the table
the table below display the columns in html using mysql fetch array. all data is displayed.
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
my purpose is embed the php in html in another way using msqli
echo "<tr>";
echo '<td>' . $row['CustomerID'].'</td>';
echo '<td>' . $row['Name']. '</td>';
echo "</tr>";
}
echo "</table>";
?>
my purpose is embed the php in html in another way using msqli
Just replace all the mysql with mysqli
$result = mysqli_query("SELECT * FROM customer");
while($row = mysqli_fetch_array( $result )) {
If you wanna keep it simple
Or do it like this...
/*Your connection info goes here...*/
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM customer";
if ($result = $mysqli->query($query)) {
echo "<table ><tr><th>ID</th><th>Name</th></tr>";
/* fetch results by row */
while ($row = $result->fetch_row()) {
echo "<tr><td>' . $row['CustomerID'].'</td><td>' . $row['Name']. '</td></tr>';
}
/* free the result set */
$result->close();
}
/* close the connection */
$mysqli->close();