ini file not being ready properly - php

I keep getting this error:
Warning: mysql_connect(): Unknown MySQL server host 'elephant' (1) 4
My ini file is this:
[Database]
DBHostName = "localhost"
DatabaseName = "elephant"
DBUserName = "elephant"
DBPassword = "practice"
[Config]
ConfigTableName = "stage2_config"
Skin="purple"
My file that reads the ini file and gives the error is this on the line with the double bold stars. I have no idea what is going on here and why it says the host isn't localhost.
<?php
$gv_SystemTest = "<h2>system.php has been included</h2>";
$gv_SystemFileInfo = pathinfo(__FILE__);
$gv_SystemDirectory = $gv_SystemFileInfo['dirname'] . '/';
$gv_SystemINIpath = $gv_SystemDirectory . '';
$gv_SiteGlobals = parse_ini_file($gv_SystemINIpath . $_SERVER['WIA_SYSTEM_INI'], true);
function echoContent($p_ContentID) {
global $gv_SiteGlobals;
$fv_dbLink = mysql_connect($gv_SiteGlobals['Database']['DBHostName'],
$gv_SiteGlobals['Database']['DBUserName'],
$gv_SiteGlobals['Database']['DBPassword']);
**mysql_connect($gv_SiteGlobals['Database']['DatabaseName']);**
$fv_TheQuery = "select *from wia_content where content_id = $p_ContentID";

The function mysql_connect does not allow to select a database as you tried on this line.
mysql_connect($gv_SiteGlobals['Database']['DatabaseName']);
To connect and select a database, you need to use the function mysql_select_db in addition to mysql_connect.
$link = mysql_connect('host','username','password');
$selected = mysql_select_db('database_name', $link);
http://php.net/manual/en/function.mysql-select-db.php

Dont use mysql use only mysqli or pdo
I think you want to select db
to select db you should be mysql_select_db()
mysql_select_db('foo', $link);
your error in this line
mysql_connect($gv_SiteGlobals['Database']['DatabaseName']);
it should be of the form
mysql_connect('localhost', 'mysql_user', 'mysql_password');

Related

mysql vs mysqli error when running php code in wamp stack

( mysql vs mysqli error (advait) )
Hi All, See code. This code runs on my localhost WAMP Win7 php ver 5.5.12 but it gives an error:
---
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\get_data02.php on line 9
Call Stack
# Time Memory Function Location
1 0.0010 135216 {main}( ) ..\get_data02.php:0
2 0.0010 135680 mysql_connect ( ) ..\get_data02.php:9
---
I tried replacing mysql with mysqli but that just gave more errors. How do I fix this? Thanks,
<?php
// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'iamdb_copy_04';
// 2. Create a database connection
$connection = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
// 3. Select a database to use
$db_select = mysql_select_db($dbname,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$query = mysql_query("SELECT * FROM users WHERE city = 'city' ");
while ($rows = mysql_fetch_array($query)) {
$f_name = $rows['first_name'];
$address_01 = $rows['address_01'];
$city = $rows['city'];
echo "$f_name<br>$address_01<br>$city<br><br>";
}
?>
There are two options to get out from this error.
Set display_errors off in php file using ini_set("display_errors",0) after start of php <?php.
Replace mysql with mysqli in mysql_connect, mysql_select_db,mysql_query, mysql_fetch_array.
Let me know if you still face any problem and also comment your error.

cannot get a connection to MySQL in php

I have this simple code:
<?php
//Open the mySQL connection
$conn_string = "'localhost', 'Vale', 'test'";
$dbh = mysql_connect($conn_string);
//Check that a connection with the DB has been established
if (!$dbh)
{
die("Error in MySQL connection: " . mysql_error());
}
...
And I get the error: Error in MySQL connection: php_network_getaddresses: getaddrinfo failed: The requested name is valid, but no data of the requested type was found.
I cannot figure out what the problem is, I have been google-ing but all the suggestions have failed (tried 127.0.0.1 instead of localhost, 127.0.0.1:3306, etc.)
I have code that works with postgre, but I need to use mysql, and I am trying to modify it, but I cannot pass the first line and get a connection. Any suggestion, please? Thank you!
mysql_connect doesn't take a comma seperated string. It takes 3 individual strings.
Change it to this:
$dbh = mysql_connect($server, $mysql_user, $password);
If you absolutely have a comma separated string, and can't get around this, you can split the string like this:
$config = str_replace("'", '', $conn_string); // replace the quotes.
$config = preg_split('/,/', $conn_string); // split string on ,
if($config != $conn_string) { // make sure this returned an array
count($config) === 3 OR die("Invalid database configuration string");
$dbh = mysql_connect($config[0], $config[1], $config[2]);
if(FALSE === $dbh) {
die("Coult not connect: " . mysql_error());
}
}
You might want to consider MySQLi, instead of MySQL.
<?php
$dbh=mysqli_connect("localhost","Vale","Password","test"); /* Vale is your username? And the name of your database if test, right? And your Username's Password is blank? */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
?>
As other users have pointed out mysql_connect expects the database, username, and password as separate arguments rather than a single string.
I think another highly important issue to point out is that this particular extension is deprecated.
Please see: http://uk1.php.net/function.mysql-connect
A better solution would be to use mysqli_connect: http://uk1.php.net/manual/en/function.mysqli-connect.php
$db = mysqli_connect( 'localhost', 'Vale', 'test', 'yourDatabaseName' );
mysql_connect requires three argument not single string
$dbh = mysql_connect('localhost', 'Vale', 'test');

Having issues with my database? I am trying to have a section in my site where you can add a new neighborhood?

I am trying to add a section in my site where you can fill out a small form and it will add the neighborhood in for you. When I try to fill in the form and add the new neighborhood, I get the error below. Please let me know what I am doing wrong and why I can't get these to be added into the database? Thanks for any help!
Code:
require_once('db.php');
//set the registration variables
$name = addslashes($_POST['name']);
$builder = addslashes($_POST['builder']);
$builderURL = $_POST['builderURL'];
//connect to database
$link = mysql_connect($dbhost, $dbuser, $dbpwd);
if (!$link) {
die(strip_tags('Could not connect: '.mysql_error()));
}
//add this contest to the database
$stmt = "INSERT INTO hf_neighborhoods (name, builder, builder_url, created_on) VALUES " .
"('$name', '$builder', '$builderURL', NOW())";
$result = mysql_query($dbname, $stmt);
if(!$result){
die(strip_tags('Error Adding Neighborhood: '.mysql_error()));
}
//if we made it this far, return a success
echo 'success';
?>
DB Call:
<?php
$dbhost = 'localhost';
$dbname = 'delsur_2011_dev';
$dbuser = 'delsur2011';
$dbpwd = 'newsite!';
?>
Error:
<br />
<b>Warning</b>: mysql_query() expects parameter 2 to be resource, string given in <b>/var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php</b> on line <b>21</b><br />
Error Adding Neighborhood:
Now I am receiving this error when I try to change it to that new code? Is it just not connecting to the database at all now? I am lost, I wasn't the one that set this file up, I was just told to go and try to fix it so that is what I am trying to do now? Thanks for your help
Warning: mysql_real_escape_string(): Access denied for user 'apache'#'localhost' (using password: NO) in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 8
Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 8
Warning: mysql_real_escape_string(): Access denied for user 'apache'#'localhost' (using password: NO) in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 9
Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 9
Error Adding Neighborhood: No database selected
You have to change
$result = mysql_query($dbname, $stmt);
with
$result = mysql_query($stmt, $link);
However, using mysql_* functions is deprecated and its now considered a bad practice.
Instead you should be using PDO or MySQLi!
And, dont use addslashes, at least use mysql_real_escape_string
EDIT: You should be calling mysql_real_escape_String() after you connect to the database, and not before. And also, dont forget to call mysql_select_db()!
//connect to database
$link = mysql_connect($dbhost, $dbuser, $dbpwd);
if (!$link) {
die(strip_tags('Could not connect: '.mysql_error()));
}
mysql_select_db($dbname, $link);
// Sanitize your input at least!
$name = mysql_real_escape_string($_POST['name'], $link);
$builder = mysql_real_escape_string($_POST['builder'], $link);
$builderUrl = mysql_real_escape_string($_POST['builderUrl'], $link);
//add this contest to the database
$stmt = "INSERT INTO hf_neighborhoods (name, builder, builder_url, created_on) VALUES " .
"('$name', '$builder', '$builderURL', NOW())";
$result = mysql_query($stmt, $link);
...
...
...

My php script is not using given username/pass/host rather using root#localhost (password: NO)

Got a problem! Though I found almost similar threads but none helped :(
I've written a php script to fetch the number of registered users from my MySQL database. The script is working great in my localhost; it is using the given username,pass and host name which are "root", "root", and "localhost" respectively, but the script is not using the given username/pass/host rather using root#localhost (password: NO) in Live server.
In the Live server I created a MySQL user, set an different password, and hostname there is of course not localhost. I updated the script with my newly created mysql users data. BUT, whenever I run the script, I see that the script is still using "root", "root", and "localhost"!!
take a look at the script:
//database connection
$conn = mysql_connect( "mysql.examplehost.com", "myusername", "mypass" );
$db = mysql_select_db ("regdb",$conn); //Oops, actually it was written this way in the script. I misstyped it previously. now edited as it is in the script.
//Query to fetch data
$query = mysql_query("SELECT * FROM regd ");
while ($row = mysql_fetch_array($query)):
$total_regd = $row['total_regd'];
endwhile;
echo $total_regd;
-- Some says to change the default username and pass in the config.ini.php file located in phpMyAdmin directory. Would this help?? I didn't try this because either my hosting provider didn't give me privilege to access that directory (because I am using free hosting for testing scripts) or I simply didn't find it :(
Please help....
Foreword: The MySQL extension is marked as deprecated, better use mysqli or PDO
Though you store the connection resource in $conn you're not using it in your call to mysql_query() and you're not checking the return value of mysql_connect(), i.e. if the connection fails for some reason mysql_query() "is free" to establish a new default connection.
<?php
//database connection
$conn = mysql_connect( "mysql.examplehost.com", "myusername", "mypass" );
if ( !$conn ) {
die(mysql_error()); // or a more sophisticated error handling....
}
$db = mysql_select_db ("regdb", $conn);
if ( !$db ) {
die(mysql_error($conn)); // or a more sophisticated error handling....
}
//Query to fetch data
$query = mysql_query("SELECT * FROM regd ", $conn);
if (!$query) {
die(mysql_error($conn)); // or a more sophisticated error handling....
}
while ( false!=($row=mysql_fetch_array($query)) ):
$total_regd = $row['total_regd'];
endwhile;
echo $total_regd;
edit: It looks like you're processing only one row.
Either move the echo line into the while-loop or (if you really only want one record) better say so in the sql statement and get rid of the loop, e.g.
// Query to fetch data
// make it "easier" for the MySQL server by limiting the result set to one record
$query = mysql_query("SELECT * FROM regd LIMIT 1", $conn);
if (!$query) {
die(mysql_error($conn)); // or a more sophisticated error handling....
}
// fetch data and output
$row=mysql_fetch_array($query);
if ( !$row ) {
echo 'no record found';
}
else {
echo htmlspecialchars($row['total_regd']);
}
First of all:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
What is your mysql_error()? :)

Connecting remote SQL Server to PHP

Can any one explain the connection string from the parameters below:
server IP : 192.168.137.4
Windows Authentication : Windows Authentication
UserName : DELL-M102Z\dell
Database : DataProd
Network Protocol : <default>
Product Name : Microsoft SQL Server Express Edition
Server Name : DELL-M102Z\SQLEXPRESS
Instance Name : SQLEXPRESS
Computer Name : DELL-M102Z
I tried:
$serverName = "DELL-M102Z\SQLEXPRESS"; //serverName\instanceName
$username = "DELL-M102Z\dell"; //serverName\instanceName
$conn = mssql_connect( $serverName,$username,'');
...but the result I got was:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: DELL-M102Z\SQLEXPRESS in C:...\index.php on line 17
Connection could not be established.
Can anyone tell me what is the problem here?
trying to follow this step:
http://michaelellerbeck.com/2010/03/31/cant-connect-remotely-to-sql-server-2008/
i know that i miss to activate sql browser service, and that's why i cannot connect by remote IP,
I got conclusion from that site and i must make sure that:
1. make sure you have allow network connection from sql server configuration tool
2. allow connection for this port in firewall
3. activate sql browser service
4. make sure port is listen as the the service provide
i got problem for no.3 and i'm stuck on it, this site solve the problem:
http://www.wikihow.com/Enable-Remote-Connections-SQL-2008-Express
<?php
$myServer = 'xxx.xxx.xxx.xxx:yyyy';
$myUser = 'sa';
$myPass = 'xxxxx';
$con = mssql_connect($myServer, $myUser, $myPass) or die("Could not connect to database: ".mssql_get_last_message());
if($con){
echo "connected";
}
// Select a database:
mssql_select_db('new')
or die('Could not select a database.');
// Example query: (TOP 10 equal LIMIT 0,10 in MySQL)
$SQL = "SELECT TOP 10 * FROM Table";
// Execute query:
$result = mssql_query($SQL)
or die('A error occured: ' . mysql_error());
// Get result count:
$count = mssql_num_rows($result);
print "Showing $count rows:<hr/>\n\n";
// Fetch rows:
while ($Row = mssql_fetch_assoc($result)) {
print $Row['BillNo'] . "\n";
}
mssql_close($con);
$myServer = 'IP_Address:PORT';
Here semicolon is need to ip and port
and
Mssql is enbaled at your cpanel

Categories