Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Any one knows why my $result echo in footer instead of body? Is it an html maybe div problem or PHP?
Here is a something similar to what I have:
<?php
// 1. Create a database connection
$dbhost = "localhost";
$dbuser = "tester";
$dbpass = "12345";
$dbname = "practice";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
<?php
// 2. Perform database query
$SQLstring = "SELECT *FROM user ORDER BY username";
// Test if there was a query error
$QueryResult = #mysqli_query($connection, $SQLstring)
Or die("<p>Unable to execute query.</p>"
. "<p>Error code " . mysqli_errno($connection)
. ": " . mysqli_error($connection)) . "</p>";
?>
<!DOCTYPE>
<html>
<head>
</head>
<body>
<?php
//3. uses the mysqli_assoc() to print table
echo "<table>";
echo "<tr><th>Name</th><th>Address</th><th bgcolor='#0099FF'
align='left'>City</th><th bgcolor='#0099FF' align='left'>State</th></tr>";
$num_results = $QueryResult->num_rows;
for ($i=0; $i <$num_results; $i++) {
// 3. Use returned data (if any)
$Row = mysqli_fetch_assoc($QueryResult); //associative array
// output data from each row
echo "<tr><td>{$Row['username']}</td>";
echo "<td>{$Row['address']}</td>";
echo "<td>{$Row['city']}</td>";
echo "<td>{$Row['state']}</td>";
</tr>";
}
// 4. Release returned data - close query
mysqli_free_result($QueryResult);
?>
</body>
</html>
<?php
// 5. Close database connection
mysqli_close($connection);
?>
Any ideas? The table prints out fine no problem just not in the correct place on the site
echo "<td>{$Row['city']}</td>";
echo "<td>{$Row['state']}</td>";
</tr>"; //You got an error here!
}
I think you forgot to echo this line.
you should get parse error but i could not point that
just try change this lines
// output data from each row
echo "<tr><td>{$Row['username']}</td>";
echo "<td>{$Row['address']}</td>";
echo "<td>{$Row['city']}</td>";
echo "<td>{$Row['state']}</td>";
echo "</tr>";
}
Related
I've looked all over here. Please be patient as I am new to php and mysql.
I got WAMPP installed & seems to be working OK. I created a simple "test" database from phpMyAdmin and "firsttable" in that. I can do a simple connect using example from w3schools, but trying to select & display data I entered only throws back errors.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Connect
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT reference, firstname, lastname, room FROM firsttable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["reference"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "room:" . $row["room"]. "<br>";
}
} else {
echo "0 results";
}
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
First off, I get a parse error on line 17. The one that reads:
if ($result->num_rows > 0) {
The error says: Trying to get property of non-object.
I tried wrapping the whole php code in tags and saving it as html, but then it appeared that no row data was ever found.
I am able to use very simple code that connects successfully. I can confirm the database is in there, so is the table, and the contents I added to it.
Please, what am I doing wrong?
You need to specify the database when you connect:
$database = 'test';
$conn = mysqli_connect($servername, $username, $password, $database);
where $database is the name of your database (test in this case). MySQL doesn't know which database your table resides in without you telling it.
In addition, you should always include error checking for your database connection (you have two of these, you don't need the last one) as well as any queries. Sans this, you can check your error logs for more information when something fails.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've double-checked and everything looks closed to me, so I can't find the error. I just want to create a table to display mySQL data.
EDIT: I don't know why the closing tag was above the rest of the code, but I still get the error when it's in the correct place.
<?php
$servername = "localhost";
$username = “x”;
$password = “x”;
$dbname = “x”;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM Classroom”;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo “<tr><th>Building</th><th>Floor</th><th>Room</th><th>Instructional</th><th>Type<th>Size</th>
<th>Seating</th><th>Decking</th><th>Access</th><th>Whiteboard</th><th>Chalkboard</th></tr>”;
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo “<tr><td>”.$row[“building”].”</td></tr>”;
}
} else {
echo (“0 results”);
}
mysqli_close($conn);
}
?>
Edit: As per your original post https://stackoverflow.com/revisions/27974352/1
This should go at the very bottom:
?>
In fact, it isn't even required unless you're going to put some pure HTML after it. So leaving it out completely might save you headaches in the future.
However, several of your double-quotes look funky pasted here. You might check that they are just double-quotes, and not special characters.
These curly/smart quotes “ ” should be replaced by regular double quotes " throughout your code.
Those alone will break its functionality and cause a parse/syntax error.
Edit: As per your edit: You need to remove the last } in your file, the one just after mysqli_close($conn);. The number of braces do not match.
This works!
<?php
mb_internal_encoding('UTF-8');
$servername = "localhost";
$username = "x";
$password = "x";
$dbname = "x";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM Classroom";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<tr><th>Building</th><th>Floor</th><th>Room</th><th>Instructional</th><th>Type<th>Size</th>
<th>Seating</th><th>Decking</th><th>Access</th><th>Whiteboard</th><th>Chalkboard</th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>".$row["building"]."</td></tr>";
}
}else{
echo("0 results");
}
mysqli_close($conn);
?>
Remove ?> from all your documents, as it's unneeded, as PHP self closes at the end of a file.
I recently had my eureka moment when I finished my first database connection. After closing my browser and reopening the html form, the output suddenly changed to code instead of the database values?
HTML form:
<form action="formulier3.php" method="post">
Hoogte: <input type="text" name="height"><br>
Breedte: <input type="text" name="width"><br>
<input type="submit">
</form>
PHP Page:
<?PHP
$user_name = "root";
$password = "root";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "SELECT * FROM price WHERE height = " . $_POST["height"] . " AND width = " . $_POST["width"] . "";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
print $db_field['ID'] . "<BR>";
print $db_field['value'] . "<BR>";
print $db_field['height'] . "<BR>";
print $db_field['width'] . "<BR>";
}
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>
This is my output:
"; print $db_field['value'] . "
"; print $db_field['height'] . "
"; print $db_field['width'] . "
"; } mysql_close($db_handle); } else { print "Database NOT Found "; mysql_close($db_handle); } ?>
Does anyone knows what's going on here?
Thank you in advance!
Somehow your server has stopped processing your php pages through the php processor (apache module or fastcgi or whatever).
What you see is the effect of presenting your php code as html. The fact that you don't see all your code but rather a small part of it, it is because the part from the first < (in <?php) until the first > (in print $db_field['ID'] . "<BR>"; is being parsed by the browser as an html tag and so it is not printed. If you look at the page source you'll see the full php code.
So there has been some server-side change that has produced that php files are directly server to the browser instead of parsed by the php engine.
One possible cause, is that you are developing in your local computer and when it worked you typed in your browser something like http://localhost/your_page.php but now you are opening the php file directly from the filesystem, so the browser shows something like file:///xampp/htdocs/your_page.php. You should always open your php pages through the web server (ie. using http://localhost/....) and never by double-clicking on the file in the file explorer.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
My database is on dream host. And its unable to connect .
MY code is
<?php
$hostname = "mysql.demos.smartmobe.com"; // eg. mysql.yourdomain.com (unique)
$username = "nayacinema"; // the username specified when setting-up the database
$password = "****"; // the password specified when setting-up the database
$database = "nayacinema"; // the database name chosen when setting-up the database (unique)
$con=mysqli_connect($hostname,$username,$password,$databse);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'done';
}
$result = mysqli_query($con,"SELECT * FROM TblUsers");
print_r($result);
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
?>
it gives error like this
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/demo_smartmobe/demos.smartmobe.com/nayacinema/test.php on line 20
what may be the problem?
You have a spelling mistake.
$con=mysqli_connect($hostname,$username,$password,$databse);
Should be
$con=mysqli_connect($hostname,$username,$password,$database);
(database spelt wrong)
i have tested your code there was no connectivity.. because of spelling mistake here is correct code , i have tested on my local machine
<?php
$hostname = "localhost"; // eg. mysql.yourdomain.com (unique)
$username = "root"; // the username specified when setting-up the database
$password = ""; // the password specified when setting-up the database
$database = "test"; // the database name chosen when setting-up the database (unique)
$con=mysqli_connect($hostname,$username,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'done';
}
$result = mysqli_query($con,"SELECT * FROM user");
print_r($result);
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['user_name'];
echo "<br>";
}
?>
Thanks
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to PHP and i have been trying to create a PDF file with images from the database.All the tutorial i have seen only put an image in the Header(not from the database) and only pull text from the database to put in the PDF.i am using FPDF to create my PDF files.Any guidelines or help to achieve this will be appreciated.
Let's say that you have a table called images, where the image URLs are stored under the column url. You can use FPDF and the MySQL adapter to construct a PDF out of all of the images like this:
require 'fpdf/fpdf.php';
// DB parameters
$host = "localhost";
$user = "username";
$pass = "password";
$db = "db";
// Create fpdf object
$pdf = new FPDF('P', 'pt', 'Letter');
// Add a new page to the document
$pdf->addPage();
// Try to connect to DB
$r = mysql_connect($host, $user, $pass);
if (!$r) {
echo "Could not connect to server\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Connection established\n";
}
// Try to select the database
$r2 = mysql_select_db($db);
if (!$r2) {
echo "Cannot select database\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Database selected\n";
}
// Try to execute the query
$query = "SELECT * FROM images";
$rs = mysql_query($query);
if (!$rs) {
echo "Could not execute query: $query";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Query: $query executed\n";
}
while ($row = mysql_fetch_assoc($rs)) {
// Get the image from each row
$url = $row['url'];
// Place the image in the pdf document
$pdf->Image($url);
}
// Close the db connection
mysql_close();
// Close the document and save to the filesystem with the name images.pdf
$pdf->Output('images.pdf','F');
References
http://blog.themeforest.net/tutorials/how-to-create-pdf-files-with-php/
http://zetcode.com/databases/mysqlphptutorial/