Help me connect to my MYSQL

Status
Not open for further replies.

acellec

New Member
Messages
29
Reaction score
0
Points
0
I've created a new database using my cpanel>Mysql Databases

and then i created a php code and uplod it in my web disk public html..

how can i connect to database because i have this error

<?php $username = "acellec_acellec"; // MySQL Username $password = "francisco2"; //MySQL Password $server = "localhost"; // MySQL server you wish to connect to. Usually "localhost" $mysqlconnection = mysql_connect($server, $username, $password); if (!$mysqlconnection) { die('There was a problem connecting to the mysql server. Error returned: '. mysql_error()); } ?>

my code is like this

PHP:
<?php
$username = "acellec_acellec"; // MySQL Username
$password = "kkkk"; //MySQL Password
$server = "localhost"; // MySQL server you wish to connect to. Usually "localhost"
$mysqlconnection = mysql_connect($server, $username, $password);
if (!$mysqlconnection) {
   die('There was a problem connecting to the mysql server. Error returned: '. mysql_error());
}
?>
what user name will i use? and the local host to use please help
 

Anna

I am just me
Staff member
Messages
11,752
Reaction score
581
Points
113
you need to have the database name among the parameters for connection, how else would the script know where to find the data you want to extract from the database? :)

apart for lacking database name I think you got it right, that is if you created a databse user with the name acellec through cpanel and connected that to the database with privileges needed :)
 
Last edited:

fguy64

New Member
Messages
218
Reaction score
0
Points
0
$username = "acellec_acellec"; // MySQL Username

I think there is a problem here. This looks like a database user name. When I do my mysql_connect statements, for $username I just my userid.

localhost is just that, localhost.

here is my connect statement that I used when I experimented with forms to enter the data. $id is just my hosting id that I use to log into cpanel, same for the password.

$id = $_POST["id"];
$pwd = $_POST["pwd"];
$server = "localhost";

$con = mysql_connect($server, $id, $pwd);


p.s. I hope your actual password is different from what it says in your error message.

p.s.s. Lady_Anna wirt your comment

you need to have the database name among the parameters for connection, how else would the script know where to find the data you want to extract from the database?

correct me if I am mistaken, but isn't your database name specified in the mysql_select_db statement and not the mysql_connect statement?
 
Last edited:

Anna

I am just me
Staff member
Messages
11,752
Reaction score
581
Points
113
all scripts I've encountered has that as part of the setup and initial connect... but it is possible it could be done using mysql_select_db as well
 

fguy64

New Member
Messages
218
Reaction score
0
Points
0
all scripts I've encountered has that as part of the setup and initial connect... but it is possible it could be done using mysql_select_db as well

ok cool, learn something every day!

fwiw, I learned from the php/MYSQL tutorial that is in the tutorials section of this forum, where database name is specified in the mysql_select_db as opposed to the connect.

regards.
 

acellec

New Member
Messages
29
Reaction score
0
Points
0
i have this code

PHP:
<?
    $DBConnect = mysqli_connect("localhost", "acellec", "francisco2");
    $DBConnect->select_db("acellec_AutomobileParts");
    
    $QueryResult = $DBConnect->query($SQLstring)
            Or die("<p>Unable to execute the query.</p>"."<p>Error code ". $DBConnect->errno . ": " . $DBConnect->error) . "</p>";
?>

is this the correct way? how come i still have this error

select_db("acellec_AutomobileParts"); $QueryResult = $DBConnect->query($SQLstring) Or die(" Unable to execute the query.
"." Error code ". $DBConnect->errno . ": " . $DBConnect->error) . "
"; ?>
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
The error is in your $SQLstring, probably. Could you post that?
 

acellec

New Member
Messages
29
Reaction score
0
Points
0
hello this is the code..

this is called list.php

PHP:
<body>

<?
$TableName = "automobileparts";
$SQLstring = "SELECT * acellec_automobileparts";

require_once("dbconnect.php");
        
echo "<table width='100%' border='1'>";
$Row = $QueryResult->fetch_row();
do {
        //echo "<tr><td>{$Row[0]}</td>";
        echo "<tr>";
        echo "<td align='right'>{$Row[1]}</td>";
        echo "<td align='right'>{$Row[2]}</td>";
        echo "<td align='right'>{$Row[3]}</td>";
        echo "<td align='right'>{$Row[4]}</td>";
        echo "<td align='right'>{$Row[5]}</td>";
        echo "</tr>";
        $Row = $QueryResult->fetch_row();
} while ($Row);
echo "</table>";



$DBConnect->close();


?>

</body>
and my code for the connection is this

PHP:
<?
    $DBConnect = mysqli_connect("localhost", "acellec", "francisco2");
    $DBConnect->select_db("acellec_AutomobileParts");
    
    $QueryResult = $DBConnect->query($SQLstring)
            Or die("<p>Unable to execute the query.</p>"."<p>Error code ". $DBConnect->errno . ": " . $DBConnect->error) . "</p>";
?>
now my problem is displaying it how come i cannot

error is

fetch_row(); do { //echo "{$Row[0]}"; echo ""; echo "{$Row[1]}"; echo "{$Row[2]}"; echo "{$Row[3]}"; echo "{$Row[4]}"; echo "{$Row[5]}"; echo ""; $Row = $QueryResult->fetch_row(); } while ($Row); echo ""; $DBConnect->close(); ?>
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
Code:
$SQLstring = "SELECT * acellec_automobileparts";

Needs to be

Code:
$SQLstring = "SELECT * FROM acellec_automobileparts";

You can also simplify your while loop:

Code:
while($row = $QueryResult->fetch_row()) {
....}

And it's also important to make sure you have a valid result before trying to use fetch_result:

Code:
if (!$QueryResult) {
die("Query Failed!"); }
 
Last edited:

acellec

New Member
Messages
29
Reaction score
0
Points
0
hello..

i changed my code and i have this

PHP:
<body>
<h1>Address Book</h1>
<p><a href="insert.php">Insert Data</a></p>
<?
$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts";

require_once("dbconnect.php");
        
echo "<table width='100%' border='1'>";
$Row = $QueryResult->fetch_row();
do {
        //echo "<tr><td>{$Row[0]}</td>";
        echo "<tr>";
        echo "<td align='right'>{$Row[1]}</td>";
        echo "<td align='right'>{$Row[2]}</td>";
        echo "<td align='right'>{$Row[3]}</td>";
        echo "<td align='right'>{$Row[4]}</td>";
        echo "<td align='right'>{$Row[5]}</td>";
        echo "<td align='right'><a href=\"edit.php?id={$Row[0]}\">Edit</a></td>";
        echo "<td align='right'><a href=\"delete.php?id={$Row[0]}\">Delete</a></td>";
        echo "</tr>";
        $Row = $QueryResult->fetch_row();
} while ($Row);
echo "</table>";



$DBConnect->close();


?>

i have an error like this
fetch_row(); do { //echo "{$Row[0]}"; echo ""; echo "{$Row[1]}"; echo "{$Row[2]}"; echo "{$Row[3]}"; echo "{$Row[4]}"; echo "{$Row[5]}"; echo "Edit"; echo "Delete"; echo ""; $Row = $QueryResult->fetch_row(); } while ($Row); echo ""; $DBConnect->close(); ?>


whats wrong ? is it because of the version of php? that it cannot read fetch row?


i put my list php in public_html and run but i have that error..help please
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
i have an error like this
fetch_row(); do { //echo "{$Row[0]}"; echo ""; echo "{$Row[1]}"; echo "{$Row[2]}"; echo "{$Row[3]}"; echo "{$Row[4]}"; echo "{$Row[5]}"; echo "Edit"; echo "Delete"; echo ""; $Row = $QueryResult->fetch_row(); } while ($Row); echo ""; $DBConnect->close(); ?>
That's a code fragment, not an error. What's the error message you're getting?
 

acellec

New Member
Messages
29
Reaction score
0
Points
0
That's a code fragment, not an error. What's the error message you're getting?

sorry to mislead you... i always have that code fragment.. but when i use my computer to test the code using my localhost phpadmin its doing ok..

but when i upload my php script and add database in my cpanel xhosting...
ihave that error

my cpanel user name is acellec my database name is acellec_AutomobileParts

PHP:
<?
    $DBConnect = mysqli_connect("localhost", "acellec", "francisco2");
    $DBConnect->select_db("acellec_AutomobileParts");
    
    $QueryResult = $DBConnect->query($SQLstring)
            Or die("<p>Unable to execute the query.</p>"."<p>Error code ". $DBConnect->errno . ": " . $DBConnect->error) . "</p>";
?>
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
Double check your table name. You've listed two different ones now.

Check that your user has privileges on the table as well. Go to CPanel->Mysql Databases and at the bottom of the page it will show who can access what.
 

acellec

New Member
Messages
29
Reaction score
0
Points
0
Double check your table name. You've listed two different ones now.

Check that your user has privileges on the table as well. Go to CPanel->Mysql Databases and at the bottom of the page it will show who can access what.

heres my code:

dbconnect.php

PHP:
<?
    $DBConnect = mysqli_connect("localhost", "acellec", "15556");
    $DBConnect->select_db("acellec_automobileparts");
    
    $QueryResult = $DBConnect->query($SQLstring)
            Or die("<p>Unable to execute the query.</p>"."<p>Error code ". $DBConnect->errno . ": " . $DBConnect->error) . "</p>";
?>
and heres the code for

list.php

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Automobile Parts</title>
</head>

<body>
<h1>BMK Automobile PArts</h1>
 
<form action="search1.php" method="post">
Seach for: <input type="text" name="find" /> in 
<Select NAME="field">
<Option VALUE="PartNo">Part Number</option>
<Option VALUE="PartName">Part Name</option>
<Option VALUE="Description">Description</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?
$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts";

require_once("dbconnect.php");
        
echo "<table width='100%' border='1'>";
$Row = $QueryResult->fetch_row();
do {
             echo "<tr><td>{$Row[0]}</td>";
        
        echo "<td align='right'>{$Row[1]}</td>";
        echo "<td align='right'>{$Row[2]}</td>";
        echo "<td align='right'>{$Row[3]}</td>";
        echo "<td align='right'>{$Row[4]}</td>";
        echo "<td align='right'>{$Row[5]}</td>";
                
                echo "<td align='right'><a href=\"edit.php?id={$Row[0]}\">Edit</a></td>";
        echo "<td align='right'><a href=\"delete.php?id={$Row[0]}\">Delete</a></td>";
        echo "</tr>";
        $Row = $QueryResult->fetch_row();
} while ($Row);
echo "</table>";






$DBConnect->close();


?>
<p><a href="insert.php">Add Parts</a></p>
</body>
</html>
i uploaded this php script in my xhosting public_html

how come i have this fragment error

BMK Automobile PArts

Seach for: in Part Number Part Name Description "; $Row = $QueryResult->fetch_row(); do { echo "{$Row[0]}"; echo "{$Row[1]}"; echo "{$Row[2]}"; echo "{$Row[3]}"; echo "{$Row[4]}"; echo "{$Row[5]}"; echo "Edit"; echo "Delete"; echo ""; $Row = $QueryResult->fetch_row(); } while ($Row); echo ""; $DBConnect->close(); ?>
Add Parts

my cpanel username is acellec
 
Status
Not open for further replies.
Top