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");
Related
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>
I have a file login.php to try to connect to my database.
I put the file login.ph inside the server folder and started the server.
Then i call the file in the browser and it shows a blank page. It does not respond even if i change the values of the database to an incorrect value.
I don't know if the error is inside the code or if it is another problem.
Thanks.
login.php:
<?php
$username = $_GET['fname'];
$password = $_GET['fpass'];
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$qz = "SELECT contact_id FROM contacts" ;
$qz = str_replace("\'","",$qz);
$result = mysqli_query($con,$qz);
while($row = mysqli_fetch_array($result))
{
echo $row['contact_id'];
}
mysqli_close($con);
?>
You must check $con variable that you have set with the result of the connection:
if (!$con) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
Do you have web server (say apache) installed and running on your server? You have to. Then put your file on the web server folder (say /var/www/html) and test in the browser.
I'm a newbie using phpmyAdmin on a NAS drive. I have a simple html page which calls a php file (CommitteeExtract.php) to retrieve data from a mySQL database on the server. While developing I created a number of versions of the php file. I eventually got it working on version 8 (CommitteeExtract8.php). I now want to rename it to CommitteeExtract.php. However, when I do so the generated html file looks and works like the original (failed) verison. I have noticed renaming the file to any previous version give the result from that version, even if the code in the file is version 8.
Is the server caching the old versions and ignoring the current content? How do I get it to recognise only the current version.
I hope this is clear, any help would be appreciated:
CommitteeExtract.php looks like this:
<html>
<head>
<link rel="stylesheet" href="Style_Main.css" >
<title>Details</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Birthday</th>
<th>Email</th>
<th>Street</th>
<th>Town</th>
<th>MobilePhone</th>
</tr>
<?php
$servername = "xxx.xxx.x.xxx";
$username = "User";
$password = "1234";
$dbase = "mydatabase";
// Create connection
$conn = mysql_connect($servername, $username, $password) or die("Cannot ccnnect to server");
mysql_select_db($dbase) or die("Could not connect to $dbase database");
clearstatcache();
$query="SELECT FullName, DateOfBirth, EmailAddress, Street, Town, City, County, PostCode, MobilePhone FROM mydatabase'";
$result = mysql_query($query) or die("Query Data Extract failed");
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['FullName'] . "</td>";
echo "<td>" . $row["DateOfBirth"] . "</td>";
echo "<td>" . $row["EmailAddress"] . "</td>";
echo "<td>" . $row["Street"] . "</td>";
echo "<td>" . $row["Town"] . "</td>";
echo "<td>" . $row["MobilePhone"] . "</td>";
echo "</tr>";
}
mysql_free_result($result);mysql_close($link);
?>
</table>
</body>
<p>There's a button down here! Waheyyyy</p>
<button type="Button" onclick="ExportToExcel()">Export to Excel</button>
</html>
it is called from a menu item in a list by this
<li class="dropdown">Export Data
<div class="dropdown-content">
Gmail Contacts
Committee Extract
Produce Forms
</div>
</li>
When the href contains CommitteeExtract8.php, and the file is called CommitteeExtract8.php it works fine, returning my data.
However If rename the file to CommitteeExtract.php and change the href to CommitteeExtract.php it fails with same errors as the original CommitteeExtract.php, even though the code is completely different.
I'm sorry, I don't know how to view and post logs
I'm trying to learn how to make mysql queries, using MAMP, MariaDB, and 2 PHP scripts.
I used MYSQL Workbench to make a schema (helloWorld), added a table (Paintings), and put 2 entries in the table. I created a 'shuwoo'#'localhost' user, and granted all permissions to it, using the terminal. I successfully logged into MYSQL Workbench, but when I looked at shuwoo's "Administrative Roles", "DBA", "SecurityAdmin", and "DBManager" are unchecked. Trying to enable them in the Workbench gives me an error.
The real issue is that my php script gives me an error, "Could not connect: Access denied for user 'shuwoo'#'localhost' (using password: YES)" when I run the script. The code is very simple, here it is:
Note: I've noticed it doesn't matter what I enter for mysql_select_db. I think that's because I'm not even getting access to this level?
Your help is greatly appreciated!
<?php
$dbhost = 'localhost:3306';
$dbuser = 'shuwoo';
$dbpass = 'Poptart5';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM Paintings';
mysql_select_db('helloWorld');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "ID :{$row['Id']} <br> ".
"Title: {$row['Title']} <br> ".
"Description: {$row['Description']} <br> ".
"Medium: {$row['Medium']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully";
mysql_close($conn);
?>
Okay i`m loading images from a MySql Database server.. but when i click on it, it opens in a new page not in the lightbox popup.
// Make the connect to MySQL or die
// and display an error.
$db = mysql_connect($host, $username, $password);
if (!$db) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
$categorie = $_GET['categorie'];
echo "<h3>" . $categorie . "</h3><br><br>";
$query = "SELECT * FROM afbeelding WHERE categorie = '" . $categorie . "'";
$resultaat = mysql_query($query,$db) or die(mysql_error());
echo "<table>";
while ($rij = mysql_fetch_array($resultaat)){
echo "<img src=" . $rij["afbeelding"] . " width=\"150px\" height=\"150px\" class='foto' />";
}
echo "</table>";
?>
Please some help
If you're loading you images after the page is loaded, you might want to call the function that initiates the lightbox after the images arrive.
You need to set correct CSS class for your <A>, otherwise lightbox JS will not take care of it.