Solved

When trying to connect to database get error message

Posted on 2013-12-05
18
1,234 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
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 109

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 109

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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now