?
Solved

When trying to connect to database get error message

Posted on 2013-12-05
18
Medium Priority
?
1,506 Views
Last Modified: 2013-12-05
$link = mysql_connect('localhost', 'root', 'root');

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 25
0
Comment
Question by:burnedfaceless
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698597
What is the code at line 25?
0
 

Author Comment

by:burnedfaceless
ID: 39698612
$link = mysql_connect('localhost', 'root', 'root');
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698621
Sorry but the error says mysqli_connect and that line says mysql_connect: are we missing something here?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39698629
Please follow the guidelines in this article.  Choose MySQLi or PDO.  Don't use MySQL any more.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:burnedfaceless
ID: 39698658
Thanks Ray.

I'm still getting the following error and I thought maybe my syntax was wrong.

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 25

$link = mysqli_connect('localhost', 'root', 'root');
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698692
Except usually one has to declare the database and since probably you're using select_db function, your syntax is right. I presume you'reading the last edition of the Yank's book, since I have the code of an older edition and in chapter 4 there is no index.php. Can you post the index.php whole code?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39698693
0
 

Author Comment

by:burnedfaceless
ID: 39698753
Recently on my mac. I have the fourth edition. I can access the database through the terminal.

<?php
if (get_magic_quotes_gpc())
{
      function stripslashes_deep($value)
      {
            $value = is_array($value) ?
                        array_map('stripslashes_deep', $value) :
                        stripslashes($value);

            return $value;
      }

      $_POST = array_map('stripslashes_deep', $_POST);
      $_GET = array_map('stripslashes_deep', $_GET);
      $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
      $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}

if (isset($_GET['addjoke']))
{
      include 'form.html.php';
      exit();
}

$link = mysqli_connect('localhost', 'root', 'password');
if (!$link)
{
      $error = 'Unable to connect to the database server.';
      include 'error.html.php';
      exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
      $output = 'Unable to set database connection encoding.';
      include 'output.html.php';
      exit();
}

if (!mysqli_select_db($link, 'ijdb'))
{
      $error = 'Unable to locate the joke database.';
      include 'error.html.php';
      exit();
}

if (isset($_POST['joketext']))
{
      $joketext = mysqli_real_escape_string($link, $_POST['joketext']);
      $sql = 'INSERT INTO joke SET
                  joketext="' . $joketext . '",
                  jokedate=CURDATE()';
      if (!mysqli_query($link, $sql))
      {
            $error = 'Error adding submitted joke: ' . mysqli_error($link);
            include 'error.html.php';
            exit();
      }

      header('Location: .');
      exit();
}

$result = mysqli_query($link, 'SELECT joketext FROM joke');
if (!$result)
{
      $error = 'Error fetching jokes: ' . mysqli_error($link);
      include 'error.html.php';
      exit();
}

while ($row = mysqli_fetch_array($result))
{
      $jokes[] = $row['joketext'];
}

include 'jokes.html.php';
?>
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698761
Please check the path of included file 'form.html.php'
0
 

Author Comment

by:burnedfaceless
ID: 39698803
It's in the same directory.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698839
So your directory tree is something like:

/Library/
    WebServer/
        Documents/
            yank/
                chapter4/
                    addjoke/
                        index.php
                        form.html.php
                        output.html.php
                        error.html.php

It is right?
I'm sorry for this inquiry, but I don't see errors in the script and I'm trying to guess what's going wrong...
0
 

Author Comment

by:burnedfaceless
ID: 39698896
yes sir

documents is "local host"
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698939
Mmmmh: in the code of the old edition I read, Yank named his file jokes.php, not jokes.html.php: have you renamed that?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39698955
From http://us3.php.net/manual/en/mysqli.construct.php ...

If you do Not have a default database set up in 'php.ini', the statement is expecting the name of the database to be the fourth parameter.

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
0
 

Author Comment

by:burnedfaceless
ID: 39699091
Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 25

Warning: mysqli_set_charset(): Couldn't fetch mysqli in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 34

Warning: include(output.html.php): failed to open stream: No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 37

Warning: include(): Failed opening 'output.html.php' for inclusion (include_path='.:') in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 37

I hate to say it but should I just work through this book from an actual server rather than my computer?
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 39699129
Well, that could be a workaround: personally, I leaved localhost since I got several problem when I uploaded my sites/scripts on the server and I hade to adjust paths, external libs and so on: so now I work always on my web server set as production server will be.

But this wouldn't explain whay you're getting all those errors. It really seems something wrong in your localhost paths. Anyway, differently from I thought (comment ID 39698692), you never use select_db function so you should add your database name to your connection line:

$link = mysqli_connect('localhost', 'root', 'password', 'database_name');

I'm sorry I can't give you a better help
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39699180
Thanks for points, but I fear they are undeserved: what is the solution?
0
 

Author Comment

by:burnedfaceless
ID: 39699242
Use the database on the web server.

It will be less work in the long run.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question