Hi I'm having problems accessing my server i'm using mysql on my laptop apache2 and php. my problem is i can seem to connect to db but can't get any data out of the registered table inside the db. Thanks in advance always i'm running window7.
<?php
$username = "root";
$password = "deslap";
$hostname = "localhost";
#connection to the database seems to work and prints connected to MySQL
$dbhandle = mysql_connect($hostname, $username, $password)or die("Unable to connect to MySQL");
echo "<br />Connected to MySQL<br>";
#select a database to work with
$selected = mysql_select_db('registered',$dbhandle)or die("Could not select database");
#execute the SQL query and return records.
$result = mysql_query("SELECT id, Name FROM registered");
while($row = mysql_fetch_array($result))
{
echo "ID:".$row{'id'}." Name:".$row{'name'}."Email: ".$row{'Email'};
}
?>
</body>
</html>
Change the curly brackets to box brackets (you can use either {} or [], but square brackets are conventional for working with array elements) -
echo "ID:".$row['id']." Name:".$row['Name']."Email: ".$row['Email'];
You're also selecting only 'id' and Name (change 'name' to 'Name') so Email will not be returned.
Related
I'm trying to figure out how can I put two differents MYSQL database connections name on the same page, one is for local PC and one is for hosting server.
Does is possible to have two different databases servers name on the same page so that way not to change connection database name in a script before upload.
I have code like this for local PC
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
Can can i add another connection name in the server in same page without changing the connection database!
alidad
Yes, a single page may query as many different servers as needed:
<?php
$server1 = new mysqli("server1.example.com", "user1", "password1", "database1");
$server2 = new mysqli("server2.example.com", "user2", "password2", "database2");
$result = $server1->query("SELECT 'Hello user' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
$result = $server2->query("SELECT 'Hello user' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
The way is to have a config.ini file in your localhost, and a different one in your production environment. This way you will parse this file and get your credentials.
A better way to do that is using this library: https://github.com/vlucas/phpdotenv
It works basically on the same way, but is easy to maintain.
Here's the code:
<?php
function widget_hello_world($vars) {
$username = "database_user";
$password = "database_password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("database_name",$dbhandle)
or die("Could not select database!");
$test1 = mysql_query("select COUNT(*) FROM Table WHERE `Status`='new'");
mysql_close($dbhandle);
$content = '<table class="table">
<thead><tr><th style="text-align:left;">Title</th><th style="text-align:left;">Data</th></tr></thead>
<tbody>
<tr><td>Test Data 1</td><td>{$test1}</td></tr>
<tr><td>Test Data 2</td><td>{$test2}</td></tr>
<tr><td>Test Data 3</td><td>{$test3}</td></tr>
</tbody>
</table>';
return array( 'title' => 'Hellow World', 'content' => $content );
}
add_hook("AdminHomeWidgets",1,"widget_hello_world");
?>
This is the error message I'm getting when I go to the page:
Connected to MySQL
Unexpected input field parameter in database query.
Obviously it's connecting to the database, but it says there's an issue with the query.
At first I thought the issue was with this line:
$test1 = mysql_query("select COUNT(*) FROM Emails WHERE `Status`='new'");
However, even when I delete that line, the same error keeps on happening. What am I doing wrong?
It seems this error occurs on mysql_close($dbhandle);
However, I also notice that reserved word "Table" should be back-tick to prevent an invalid query. Perhaps something in your callback routine is global and affecting the connection.
Also, use this:
echo mysql_error();
to see any mysql errors during query.
Try to do a separate checking for the DB being selected, like:
<?php $con = mysql_connect('localhost', 'root', 'mypass');
$selected_db = mysql_select_db('DB Name here');
if (!$selected_db)
{
die ('Database not selected : ' . mysql_error());
}
?>
If it says 'Database is not selected' then check the spelling of your Database inside the mysql_select_db(). You could also comment the mysql_close(); just to see if error will still persist.
On the other note it is better to use mysqli_ because mysql_ is deprecated or better yet use PDO.
My php code is below:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$db="newsportaldb";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db($db,$dbhandle)
or die("Could not select DB");
$sql= mysql_query('select username from tbl_login');
while($rss= mysql_fetch_array($sql))
{
$ress["homepagecontents"][] = array("titles"=>$rss['username']);
}
echo json_encode($ress);
mysql_close($dbhandle);
?>
//in this case we getting "?????????????????" as a result of echo json_encode($ress);
The Question marks come from wrong charset.
Make sure that the connection has right charset (utf8).
If on phpmyadmin you dont see the data correctly so change the db,table and column to utf8_general_ci.
Hot recommend for you: upgrade your code and use php PDO Object.
Good luck !
I am trying to make a blog site.For this purpose I need to use a specific data from a specific field from my database table.To do that I wrote these code.
<?php
$host = "localhost";
$user = "root";
$pass = "12345";
$db = "bnsb";
$conn = mysql_connect($host, $user, $pass) or die("Connection Failed!");
mysql_select_db($db, $conn) or die("Database couldn't select!");
$img = "select image from news where uid=1";
echo $img;
?>
My database connection is OK.It should print like this user_img1.jpg. But it prints the whole sql query like select image from news where uid=1. I run this code on phpmyadmin. It works! But it does not work in my php script.How can I do now?
You can not give the query as it is and expect result like in phpadmin.
For this first of all you have to connect to your DB like this
$con = mysqli_connect("localhost","my_user","my_password","my_db");
execute required query like this
$query22 = "select image from news where uid = 1";
$result22 = mysqli_query($con, $query22) or die (mysqli_error());
Get the result and display like this
while($rows = mysqli_fetch_array($result22, MYSQLI_BOTH))
{
echo "<br>Values in db: " . $rows['columnname'];
}
Also i advice you to take a look at these tutorials
http://codular.com/php-mysqli
http://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/
Please read some PHP 101 kind of tutorials on how to use PHP.
To get data from DB (in almost any language)
You need to connect to a DB. The connection gets you some sort of resource
You formulate your query (which you seem to have done)
You execute the query against the DB that you connected to (step #1)
You get a result (set)
You iterate over the result set to get the individual result(s); in your case the result set would be just one result (or row).
The examples to do this in PHP are very basic; please do your own lookup on net. This one seems good enough to get you started - http://www.w3schools.com/php/php_mysql_intro.asp
Try this,
<?php
$host = "localhost";
$user = "root";
$pass = "12345";
$db = "bnsb";
$conn = mysql_connect($host, $user, $pass) or die("Connection Failed!");
mysql_select_db($db, $conn) or die("Database couldn't select!");
$img = "select image from news where uid=1";
$result=mysql_query($img);
while($row=mysql_fetch_array($result)){
echo '<img src="your_path_to_image/'.$row['image'].'" /> - '.$row['image'];
}
?>
I am a complete database newbie. So far, I know that I can connect to MySQL using PHP's mysql_connect() command, but apart from that, I really don't see how to take that data and put it onto a web page.
a) are there ways other than mysql_connect()
b) lets say I had a table of data in mysql and all I wanted was for that table (for example: list of names and telephone numbers) to now appear on my web page. I can't for the life of me find a tutorial for this.
<?
$database_name = "dbname";
$mysql_host = "localhost"; //almost always 'localhost'
$database_user = "dbuser";
$database_pwd = "dbpass";
$dbc = mysql_connect($mysql_host, $database_user, $database_pwd);
if(!$dbc)
{
die("We are currently experiencing very heavy traffic to our site, please be patient and try again shortly.");
}
$db = mysql_select_db($database_name);
if(!$db)
{
die("Failed to connect to database - check your database name.");
}
$sql = "SELECT * FROM `table` WHERE `field`= 'value'";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
// code here
// access variables like the following:
echo $row['field'].'<br />';
echo $row['field2'];
}
?>
Check out mysql_fetch_assoc mysql_fetch_array and mysql_fetch_object
This is the very basics, you will want to search for tutorials. There are many about.