I have Ubuntu 16.10 x86_64 x86_64. I installed LAMP to program in PHP and to create databases. In my php program I want to connect to my local database for creating a table ( in HTML ) with the data of any row of the table.
The problem is that when I open the php file( localhost/file.php ) through firefox ,the browser doesn't charge anything. If thare were been an error during the connection with the database, It would have printed something in the browser.
Here the code:
<!DOCTYPE html>
<html>
<head><title> SQL & PHP </title></head>
<body>
<?php
$db = mysql_connect("localhost", "root", "password")
or die ("Non riesco a creare la connessione");
mysql_select_db("scuola")
or die ("Non trovo il DB");
$sql = "SELECT id_utente, nome_utente, password_utente, conta_pres FROM utenti WHERE conta_pres <> 0";
$ris = mysql_query($sql) or die ("Query fallita!");
echo "<TABLE><TR><TH>ID utente <TH> Nome utente <TH>Password<TH>Contatore visite</TR>";
while ($riga= mysql_fetch_array($ris))
{
echo ("<TR>");
echo "<TD>" . $riga["id_utente"];
echo "<TD>" . $riga["nome_utente"];
echo "<TD>" . $riga["password_utente"];
echo "<TD>" . $riga["conta_pres"];
}
mysql_close();
?>
</body>
</html>
I checked the syntax (using a website) of the code and thare aren't problems,even because I copied this one by a book. I read that mysql_connect has been deprecated, so I replaced it with new mysqli_connect but the error still remains: white page. I tried to put 2 echo, one before the connecting function and one after that. Only the first echo is printed on the screen. I tried to type in the terminal sudo apt-get install php5-mysql but there is an error:
The "php5-mysql" packet has not run to install
Can someone help me, please?
First of all use mysqli instead of mysql.
I think I have found the problem. When you call mysqli_select_db, it expects 2 parameters and you only specified one. Even though you have set the $db database connection, you need to specify which database you want to select the database name from.
So mysqli_select_db($db, "scuola") should do the trick.
And at the bottom close the connection specifying which connection to close. In your case it is: mysqli_close($db);
<!DOCTYPE html>
<html>
<head><title> SQL & PHP </title></head>
<body>
<?php
$db = mysqli_connect("localhost", "root", "password")
or die ("Non riesco a creare la connessione");
mysqli_select_db($db, "scuola") // see this line
or die ("Non trovo il DB");
$sql = "SELECT id_utente, nome_utente, password_utente, conta_pres FROM utenti WHERE conta_pres <> 0";
$ris = mysql_query($sql) or die ("Query fallita!");
echo "<TABLE><TR><TH>ID utente <TH> Nome utente <TH>Password<TH>Contatore visite</TR>";
while ($riga= mysql_fetch_array($ris))
{
echo ("<TR>");
echo "<TD>" . $riga["id_utente"];
echo "<TD>" . $riga["nome_utente"];
echo "<TD>" . $riga["password_utente"];
echo "<TD>" . $riga["conta_pres"];
}
mysqli_close($db); // see this line
?>
</body>
</html>
Related
im trying to connect php and msaccess
Im using the code below in connecting php and ms access and so far it working fine when i add System DSN to fetch database locally
but when the database is in remote location i receive the message connection failed
<html>
<body>
<?php
$conn=odbc_connect('northwind','','');
if (!$conn)
{exit("Connection Failed: " . $conn);}
$sql="SELECT * FROM customers";
$rs=odbc_exec($conn,$sql);
if (!$rs)
{exit("Error in SQL");}
echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";
while (odbc_fetch_row($rs))
{
$compname=odbc_result($rs,"CompanyName");
$conname=odbc_result($rs,"ContactName");
echo "<tr><td>$compname</td>";
echo "<td>$conname</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
</body>
</html>
Just for information im working this is old server with winserver 2003 installed
I have a MySQL database on my local machine and am trying to query it with PHP.
I've written out the following HTML/PHP code. When I open this HTML file in Chrome, this is what is displayed. Why does the browser treat this code as text?
ID Project Code'; while ($row = mysqli_fetch_array($response)){ echo '' . $row[Id] . '' . $row[Project_Code] . ''; echo ''; } echo ''; } else { echo "couldn't connect do it"; echo mysqli_error($dbc); } mysqli_close($dbc); ?>
code:
<HTML>
<body>
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'my_local_ip');
DEFINE ('DB_NAME', 'my_db');
$dbc = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("couldn't connect");
$query = "SELECT Id, Project_Code FROM database.projects";
$response = #mysqli_query($dbc, $query);
if ($response){
echo '<table>
<td>ID</TD>
<td>Project Code</TD></table>';
while ($row = mysqli_fetch_array($response)){
echo '<tr><td>' .
$row[Id] . '</td></td>' .
$row[Project_Code] . '</td></td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "couldn't connect do it";
echo mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</BODY>
</HTML>
It is Because you are not running your code with apache/php/mysql.
If you are using windows package like wamp put it in www directory and access it through localhost, not directly open it in the browser, otherwise browser will treat it like plain html.
i've opened it as you were opening in chrome:
You should access your script using localhost and make sure your wamp (or whatever server package your are using) should be running.
PHP unlike javascript/HTML is a server side language. It runs on the server and outputs the generated content to the browser.
So for example
<?php
if(empty($a)) {
echo ':)';
} else {
echo ':(';
}
?>
The only part that makes it to the browser would be :).
The issues you are running into are
a) your file is .html. PHP won't run as such unless your server configuration is modified to do so.
b) your files are being executed on your local machine which doesn't have PHP installed.
Solutions:
a) Install PHP on your local machine; http://php.net/manual/en/install.php
b) Move to a hosted server (this might be easier since they will have support for configuration issues)
(lettering unrelated to issues lettering)
To be more specific your whole PHP code is outputted to the browser but the
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'my_local_ip');
DEFINE ('DB_NAME', 'my_db');
$dbc = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("couldn't connect");
$query = "SELECT Id, Project_Code FROM database.projects";
$response = #mysqli_query($dbc, $query);
if ($response){
echo '<table>
Is all read as one element <?php until the > of the <table>.
Also while learning try to never use the #. That suppresses errors and those might (95% of the time, will) be useful to you.
After you installed Apache,MySQL and PHP
(You can try XAMPP or WAMP as mentioned in other answers.)
you can test this code:
(For XAMPP put your code in xampp\htdocs folder ,
name like something.php
to run. browse to localhost/something.php)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'database');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("couldn't connect");
$query = "SELECT Id, Project_Code FROM database.projects";
$response = mysqli_query($dbc, $query);
?>
<?php
if ($response){
echo '<table>
<td>ID</td>
<td>Project Code</td>';
while ($row = mysqli_fetch_array($response)){
echo '<tr>'.'<td>'.$row['Id'].'</td><td>'.$row['Project_Code'].'</td></tr>';
}
echo '</table>';
} else {
echo "couldn't connect do it";
echo mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</BODY>
</HTML>
ok so I have the following code that I am running in the python shell:
import MySQLdb
db = MySQLdb.connect(host = "xxxx",user="xxxx"password="xxxx",db="xxxx")
cur = db.cursor()
cur.execute(CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))
Fairly sure the connection part is working, I'll get an error if I enter in the wrong value, or if I deny access to the database for my computer's IP address.
on the webhosting server, I have the following basic index.php file, which I have tested on a server on my computer, and I know works. when I go to the website domain, I get the following error: "Database query failed."
Any ideas why the MySQL query isn't working? My webhosting is Cpanel with godaddy.com, should I look for something else?
<?php
$dbhost = "xxxx";
$dbuser = "xxxx";
$dbpass = "xxxx";
$dbname = "xxxx";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); /*1*/
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
<?php
$query ="SELECT * FROM qqqq"; /*2*/
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed.");
}
?>
<!DOCTYPE html PUBLIC >
<html lang="en">
<head>
<title></title>
</head>
<body>
<ul>
<?php /*3*/
while($subject = mysqli_fetch_assoc($result)){
?><li><?php echo $subject['asdf'];?></li>
<?php
}
?>
</ul>
<?php
mysqli_free_result($result); /*4*/
?>
</body>
</html>
<?php
mysqli_close($connection); /*5*/
?>
You should call db.commit() to have it complete. By default, autocommit is turned off.
You also have an error in your code. The SQL should be a string.
Shouldn't the cursor execute be calling a string? You don't have quotes around your sql statement.
This line without quotas is incorrect:
cur.execute(CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))
It should be
cur.execute("CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))")
So test in your database whether you really have table qqqq.
You could install SQL Buddy or phpMyAdmin.
I'm trying to edit an item in a mySQL database generated list. Here is the code:
<?php
// contact to database
$connect = mysql_connect("<HOST>", "<USER>", "<PASSWORD>") or die ("Error , check your server connection.");
mysql_select_db("tvc");
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
$result = mysql_query("UPDATE closet SET
pattern = '{$_POST['pattern']}'
WHERE id='{$_POST['id']}'") or die ("Error in query");
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='patterns.php'>Back to Patterns List</a>";
}
else {
echo "ERROR";
}
?>
</body>
</html>
I get an 'error in query' error message and I can't figure out what is causing it.
Any help would be much appreciated!
You forgot to remove , before WHERE
Change
$result = mysql_query("UPDATE closet SET
pattern = '{$_POST['pattern']}',
WHERE id='{$_POST['id']}'") or die ("Error in query");
To
$pattern = mysql_real_escape_string($_POST['pattern']);
$id= mysql_real_escape_string($_POST['id']);
$result = mysql_query("UPDATE closet SET
pattern = '".$pattern."' WHERE id='".$id."'") or
die("Could not connect: " . mysql_error());
Recommendations:
1.Learn to prevent from MySQL Injections: Good Link
2.Mysql extension is not recommended for writing new code. Instead, either the mysqli or PDO_MySQL extension should be used. More reading: PHP Manual
I'm trying to conntect to a mysql database for the first time. Can you see what's not working correctly below?
I get an error on line 3.
What would my server address look like from Godaddy to my database? I found that address in my control panel.
Thanks for any help. Never programmed using PHP before.
<body>
<?php
$con = mysql_connect("MydbName.db.3924516.hostedresource.com ","Userid","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Gallerys");
echo "<table border='1'>
<tr>
<th>Thumb Url </th>
<th>Gallery Url</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['THUMBURL'] . "</td>";
echo "<td>" . $row['GALLERYURL'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
Try check back the authenication settings, you might use the wrong host, username or password.
<?php
$con = mysql_connect("usually it is localhost","your MYSQL username","your MYSQL password");
// Checking the login details.
// Example of default xampp login details: $con = mysql_connect("localhost","root","");
// Xampp MYSQL default does not have password.
if (!$con) // If the login details are wrong, it will should an error.
{
die('Could not connect: ' . mysql_error());
}
?>
Without the exact error it is hard to be sure but on line 3 you have an extra space after hostedresource.com. Try removing the space between the end of the hostname and the quatation mark. Like so:
$con = mysql_connect("MydbName.db.3924516.hostedresource.com","Userid","password");